package com.mystudy.leetcode.problem.hash.p_771;

import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.HashSet;
import java.util.Set;

/**
 * @program: infoalgorithm
 * @description: 宝石与石头
 * @author: zhouzhilong
 * @create: 2019-07-10 17:33
 **/
public class Solutioin {
    /**
     * logger
     */
    private static final Logger LOGGER = LoggerFactory.getLogger(Solutioin.class);


    @Test
    public void test() {
        int result = numJewelsInStones("aA", "aAAbbbb");

        LOGGER.debug("result:{}", result);
        result = method2("aA", "aAAbbbb");
        LOGGER.debug("result:{}", result);
    }

    /**
     * 土鳖做法，效率低
     * @param J
     * @param S
     * @return
     */
    public int numJewelsInStones(String J, String S) {
        Set<Character> sets = new HashSet<>();
        for (Character c : J.toCharArray()) {
            sets.add(c);
        }
        int count = 0;
        char[] chars = S.toCharArray();
        for (Character c : chars) {
          if(sets.contains(c)){
              count++;
          }
        }
        return count;
    }

    /**
     * 双重循环，比第一种快点
     * @param J
     * @param S
     * @return
     */
    public int method2(String J, String S){
        char[] jChars = J.toCharArray();
        char[] sChars = S.toCharArray();
        int count = 0;
        for(int i = 0;i < jChars.length;i++){
            for (int j = 0; j < sChars.length; j++) {
                if(jChars[i] == sChars[j]){
                    count++;
                }
            }
        }
        return count;
    }


}
